package com.backstage.productModule.controller;


import com.backstage.productModule.pojo.Details;
import com.backstage.productModule.pojo.QueryData;
import com.backstage.productModule.service.DetailsService;
import com.backstage.utils.OSSutil;
import com.backstage.utils.R;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品详细分类 前端控制器
 * </p>
 *
 * @author sakura
 * @since 2022-12-14
 */
@RestController
@RequestMapping("details")
@CrossOrigin
public class DetailsController extends BaseController{

    @Autowired
    private DetailsService detailsService;
    private static Map<String,String> map = new HashMap<>();

    /**
     * 模糊查询
     * @param queryData
     * cp  当前页   ls 当前页显示的记录数   namekw 模糊查询商品  status 是否上架 0 上架 1 下架
     * @return
     */
    @PostMapping("list")
    public R getList(@RequestBody QueryData queryData){
        Page<Details> page = new Page<>(queryData.getCp(), queryData.getLs());
        QueryWrapper<Details> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(queryData.getNamekw()))
            queryWrapper.like("info",queryData.getNamekw());
        if (!StringUtils.isEmpty(queryData.getStatus()) &&(queryData.getStatus()==0 || queryData.getStatus()==1))
            queryWrapper.eq("state",queryData.getStatus());
        detailsService.page(page,queryWrapper);
        return R.ok().data("items",page.getRecords()).data("total",page.getTotal())
                .data("current",page.getCurrent()).data("pagesize",page.getSize());
    }

    /**
     * 根据商品id删除
     * @param id
     * @return
     */
    @DeleteMapping("remove/{id}")
    public R removeByID(@PathVariable Long id){
        return detailsService.removeById(id)?R.ok():R.error();
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @DeleteMapping("batchRemove/{ids}")
    public R batchIds(@PathVariable List<Long> ids){
        return detailsService.removeByIds(ids)?R.ok():R.error();
    }

    /**
     * 修改商品信息
     * @param details
     * @return
     */
    @PostMapping("update")
    public R edit(@RequestBody Details details){
        return detailsService.updateById(details)?R.ok():R.error();
    }

    /**
     * 处理商品上架
     * @param id
     * @return
     */
    @GetMapping("onOrDown/{id}")
    public R putOnShelves(@PathVariable  Long id){
        //根据ID查询当前的商品信息
        Details serviceById = detailsService.getById(id);
        //如果状态为0  则需要下架
        if (serviceById.getState()==0){
            //将状态修改为下架状态
            serviceById.setState(1);
            return detailsService.updateById(serviceById)?R.ok():R.error();
        }
        //如果状态为1  则需要上架
        if (serviceById.getState()==1){
            //将状态修改为上架状态
            serviceById.setState(0);
            return detailsService.updateById(serviceById)?R.ok():R.error();
        }
        return R.error();
    }

    /**
     * 上传文件图片
     * @param file
     * @return
     */
    @PostMapping("upload")
    public R upload(@RequestPart MultipartFile file){
        String newFileName = super.newFileName(file);
        if (file!=null && file.getSize()>0){
            String result = OSSutil.uploadObject2OSS(OSSutil.getOSSClient(),
                    file,OSSutil.BACKET_NAME,newFileName,OSSutil.FOLDER);
            System.out.println("上传文件的返回值是:"+result);
        }
        String path = "https://" + OSSutil.BACKET_NAME + "." + OSSutil.ENDPOINT1 + "/" + OSSutil.FOLDER + newFileName;
        map.put("url",path);
        return R.ok().data("url",path);
    }

    /**
     * 添加商品信息
     * @param details
     * @return
     */
    @PostMapping("save")
    public R saveProduct(@RequestBody Details details){
        String url = map.get("url");
        System.out.println("路径"+url);
        details.setUrlImage(url);
        return detailsService.save(details)?R.ok():R.error();
    }

}

